var createServer = require("http").createServer;
var myhttp = exports;

myhttp.get = function (path, handler) {
  getMap[path] = handler;
};
var NOT_FOUND = "Not Found\n";

function notFound(req, res) {
  res.writeHead(404, { "Content-Type": "text/plain"
                     , "Content-Length": NOT_FOUND.length
                     });
  res.end(NOT_FOUND);
}
var getMap = {};

myhttp.get = function (path, handler) {
  getMap[path] = handler;
};

var server = createServer(function (req, res) {

    var handler = getMap[url.parse(req.url).pathname] || notFound;
    handler.req=req;
	handler.res=res;
    res.simpleText = function (code, body) {
      res.writeHead(code, { "Content-Type": "text/plain"
                          , "Content-Length": body.length
                          });
      res.end(body);
    };

    res.simpleJSON = function (code, obj) {
      var body = new Buffer(JSON.stringify(obj));
      res.writeHead(code, { "Content-Type": "text/json"
                          , "Content-Length": body.length
                          });
      res.end(body);
    };

    handler(req, res);
	
});

myhttp.listen = function (port, host) {
  server.listen(port, host);
  sys.puts("Server at http://" + (host || "127.0.0.1") + ":" + port.toString() + "/");
};

myhttp.close = function () { server.close(); };